import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.file.Files;

import org.junit.Test;

public class ServerTestCase {
	
	
	@Test
	public void testFileCreation() throws Exception {
		final int sizeOfMegaByte = 1048576;
		
		//Delete both Long and Short test files, if they exist.
		File oldTestFile = new File(MainServer.LONG_FILE_NAME);
		if(oldTestFile.exists())
			oldTestFile.delete();
		oldTestFile = new File(MainServer.SHORT_FILE_NAME);
		if(oldTestFile.exists())
			oldTestFile.delete();
 
		//Try to generate the files
		assertEquals("The generateTestFiles() method encountered an error.", MainServer.generateTestFiles(), true);
		
		File newShortFile = new File(MainServer.SHORT_FILE_NAME);
		File newLongFile = new File(MainServer.LONG_FILE_NAME);
		
		assertEquals("The shortFile was the wrong size.", newShortFile.length(), MainServer.SHORT_FILE_MEGABYTES*sizeOfMegaByte);
		assertEquals("The longFile was the wrong size.", newLongFile.length(), MainServer.LONG_FILE_MEGABYTES*sizeOfMegaByte);
		
		

	}

	@Test
	public void testShort() throws IOException{
		Socket clientSocket = new Socket(InetAddress.getByName("127.0.0.1"), MainServer.SERVER_SOCKET_PORT);
		Socket serverSocket = MainServer.generateServerSocket();
		DataInputStream clientSocketIn = new DataInputStream(clientSocket.getInputStream());
		DataOutputStream clientSocketOut = new DataOutputStream(clientSocket.getOutputStream());
		clientSocketOut.writeChar('S');
		DataInputStream serverSocketIn = new DataInputStream(serverSocket.getInputStream());
		DataOutputStream serverSocketOut = new DataOutputStream(serverSocket.getOutputStream());
		
		char command = MainServer.receiveCommand(serverSocketIn);
		boolean quit = MainServer.executeCommand(command, serverSocketOut);
		
		long fileSize = clientSocketIn.readLong();
		byte[] buffer = new byte[1024];
		
		long bytesReceived = 0;
		for(bytesReceived = clientSocketIn.read(buffer); bytesReceived < fileSize; bytesReceived += clientSocketIn.read(buffer))
		{
		}
		
		clientSocketIn.close();
		clientSocketOut.close();
		serverSocketIn.close();
		serverSocketOut.close();
		serverSocket.close();
		clientSocket.close();
		assertEquals(quit, false);
	}
	
	public void testLong() throws IOException{
		Socket clientSocket = new Socket(InetAddress.getByName("127.0.0.1"), MainServer.SERVER_SOCKET_PORT);
		Socket serverSocket = MainServer.generateServerSocket();
		DataInputStream clientSocketIn = new DataInputStream(clientSocket.getInputStream());
		DataOutputStream clientSocketOut = new DataOutputStream(clientSocket.getOutputStream());
		clientSocketOut.writeChar('L');
		DataInputStream serverSocketIn = new DataInputStream(serverSocket.getInputStream());
		DataOutputStream serverSocketOut = new DataOutputStream(serverSocket.getOutputStream());
		
		char command = MainServer.receiveCommand(serverSocketIn);
		boolean quit = MainServer.executeCommand(command, serverSocketOut);
		
		long fileSize = clientSocketIn.readLong();
		byte[] buffer = new byte[1024];
		
		long bytesReceived = 0;
		for(bytesReceived = clientSocketIn.read(buffer); bytesReceived < fileSize; bytesReceived += clientSocketIn.read(buffer))
		{
		}
		
		clientSocketIn.close();
		clientSocketOut.close();
		serverSocketIn.close();
		serverSocketOut.close();
		serverSocket.close();
		clientSocket.close();
		assertEquals(quit, false);
	}
	
}
